//
//  TBCityIconFont.h
//  CPSCustomer
//
//  Created by 赵升 on 2020/11/07.
//  Copyright © 2020 赵升. All rights reserved.
//

#import "TBCityIconFont.h"
#import <CoreText/CoreText.h>

@implementation TBCityIconFont

//static NSString *_fontName;

+ (void)registerFontWithURL:(NSURL *)url {
    NSAssert([[NSFileManager defaultManager] fileExistsAtPath:[url path]], @"Font file doesn't exist");
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)url);
    CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    CTFontManagerRegisterGraphicsFont(newFont, nil);
    CGFontRelease(newFont);
}

- (UIFont*)customFontWithPath:(NSString*)path size:(CGFloat)size{
    NSURL *fontUrl = [NSURL fileURLWithPath:path];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
    CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    CTFontManagerRegisterGraphicsFont(fontRef, NULL);
    NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
    UIFont *font = [UIFont fontWithName:fontName size:size];
    CGFontRelease(fontRef);
    return font;
}

+ (UIFont *)fontWithSize:(CGFloat)size fontName:(NSString *)fontName resourcePath:(NSString *)path{
    NSAssert(fontName!=nil, @"fontName doesn't nil");
    NSAssert(path!=nil, @"font resourcePath doesn't nil");
#if 0
    UIFont *font = [UIFont fontWithName:fontName size:size];
    if (font == nil) {
        NSURL *fontFileUrl = [NSURL fileURLWithPath:path relativeToURL:nil];
        [self registerFontWithURL: fontFileUrl];
        font = [UIFont fontWithName:fontName size:size];
        NSAssert(font, @"UIFont object should not be nil, check if the font file is added to the application bundle and you're using the correct font name.");
    }
    return font;
#else
    UIFont *font = [UIFont fontWithName:fontName size:size];
    if(!font){
        NSURL *fontUrl = [NSURL fileURLWithPath:path];
        CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
        CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
        CGDataProviderRelease(fontDataProvider);
        CTFontManagerRegisterGraphicsFont(fontRef, NULL);
        NSString *fontOriginName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
        font = [UIFont fontWithName:fontOriginName size:size];
        CGFontRelease(fontRef);
    }
    return font;
#endif
}

//+ (void)setFontName:(NSString *)fontName {
//    _fontName = fontName;
//}
//
//
//+ (NSString *)fontName {
//    return _fontName ? : @"iconfont";
//}

@end
